// service/serveItem/serveItem.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    cheType:[],
    cate: [],
    category_id: '',
    goods: [],
    page: 1,
    finish: false,
    loading: false,
    list: [],
    skill_cate_id: '',
    safeBottom: `padding-bottom:100px`,
    city: '',
    token: ''
  },
  select(e){
    let item = e.currentTarget.dataset.item
    if(item.select === 0){
      return
    }
    let id = e.currentTarget.dataset.item.id
    let index = this.data.goods.indexOf(id)
    let goods = this.data.goods;
    let add = false
    if(index === -1){
      goods.push(id)
      add = true
    }else{
      goods.splice(index,1)
      add = false
    }
    let arr = this.data.list.map(value=>{
      if(value.id === id){
        value.select = add ? 2 : 1
      }
      return value
    })
    this.setData({
      goods,
      list: arr
    })

  },
  onChange(e) {
    this.setData({
      category_id: this.data.cate[e.detail.index].id
    })
    this.reload()
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  back(){
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('selectGood', {goods: this.data.goods});
    util.back()
  },
  getList(){
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    http.get('goods/getapplygoods', {
      type: 1,
      city: this.data.city,
      category_id: this.data.category_id,
      page: this.data.page
    }, true, false, this.data.token).then(res => {
      if(res.data.length === 0){
        this.setData({
          finish: true
        })
      }
      let data = res.data.map(value => {
        if(this.data.category_ids.indexOf(this.data.category_id) === -1){
          value.select = 0
        }else if(this.data.goods.indexOf(value.id) === -1){
          value.select = 1
        }else{
          value.select = 2
        }
        return value
      })
      let arr = this.data.list.concat(data)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  
  getInfo(){
    http.post('category/getlist', '', true, false, this.data.token).then(res => {
      this.setData({
        cate: res.data,
        category_id: res.data[0].id
      })
      this.getList()
    })
  },
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `padding-bottom:${high}px`
    })
    let arr = options.goods_ids ? options.goods_ids.split(',') : []
    let goods = arr.map(value => {
      return Number(value)
    })
    let cate = options.category_ids ? options.category_ids.split(',') : []
    let category = cate.map(value => {
      return Number(value)
    })
    this.setData({
      category_ids : category,
      goods: goods,
      city: options.city,
      token: options.token || ''
    })
    
    this.getInfo()
  },

  
})